﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HtmlAgilityPack;

namespace YahooFinanceScraper
{
    public class CompanyInfo
    {
        public int PortfolioId { get; set; }
        public string Symbol { get; set; }
        public string Url { get; set; }

        public string PrevClose { get; set; }
        public string Open { get; set; }
        public string Bid { get; set; }
        public string Ask { get; set; }
        public string TargetEst { get; set; }
        public string Beta { get; set; }
        public string NextEarningsDate { get; set; }

        public string DaysRange { get; set; }
        public string Wk52Range { get; set; }
        public string Volume { get; set; }
        public string AvgVol3m { get; set; }
        public string MarketCap { get; set; }
        public string PE { get; set; }
        public string EPS { get; set; }
        public string Div { get; set; }

        public static CompanyInfo ParseCompany(string companyName)
        {
            string url = string.Format("http://finance.yahoo.com/q?s={0}", companyName);

            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(url);

            CompanyInfo info = null;

            var infoNodes = doc.DocumentNode.SelectNodes("//div[@id='yfi_quote_summary_data']");
            if (infoNodes != null)
            {
                foreach (HtmlNode container in infoNodes)
                {
                    info = new CompanyInfo();
                    info.Symbol = companyName;
                    info.Url = url;
                    info.PortfolioId = -1;

                    /*bool isAdd = false;
                    CompanyInfo info = InfoList.FirstOrDefault(x => x.Symbol == companyName);
                    if (info == null)
                    {
                        info = new CompanyInfo();
                        isAdd = true;
                    }*/

                    HtmlNodeCollection tables = container.SelectNodes("table");
                    for (int j = 0; j < tables.Count; ++j)
                    {
                        HtmlNode table = tables[j];

                        if ((table.Id != "table1") && (table.Id != "table2"))
                            continue;

                        HtmlNodeCollection rows = table.SelectNodes("tr");
                        for (int i = 0; i < rows.Count; ++i)
                        {
                            HtmlNode row = rows[i];

                            HtmlNodeCollection cellsHeader = row.SelectNodes("th");
                            HtmlNodeCollection cellsData = row.SelectNodes("td");

                            string header = cellsHeader[0].InnerText;
                            if (header == "Prev Close:")
                                info.PrevClose = cellsData[0].InnerText;
                            else if (header == "Open:")
                                info.Open = cellsData[0].InnerText;
                            else if (header == "Bid:")
                                info.Bid = cellsData[0].InnerText.Substring(0, cellsData[0].InnerText.IndexOf(' '));
                            else if (header == "Ask:")
                                info.Ask = cellsData[0].InnerText.Substring(0, cellsData[0].InnerText.IndexOf(' '));
                            else if (header == "1y Target Est:")
                                info.TargetEst = cellsData[0].InnerText;
                            else if (header == "Beta:")
                                info.Beta = cellsData[0].InnerText;
                            else if (header == "Next Earnings Date:")
                                info.NextEarningsDate = cellsData[0].InnerText;

                            else if (header == "Day's Range:")
                                info.DaysRange = cellsData[0].InnerText;
                            else if (header == "52wk Range:")
                                info.Wk52Range = cellsData[0].InnerText;
                            else if (header == "Volume:")
                                info.Volume = cellsData[0].InnerText;
                            else if (header == "Avg Vol (3m):")
                                info.AvgVol3m = cellsData[0].InnerText;
                            else if (header == "Market Cap:")
                                info.MarketCap = cellsData[0].InnerText;
                            else if (header == "P/E (ttm):")
                                info.PE = cellsData[0].InnerText;
                            else if (header == "EPS (ttm):")
                                info.EPS = cellsData[0].InnerText;
                            else if (header == "Div &amp; Yield:")
                                info.Div = cellsData[0].InnerText;
                        }
                    }

                    /*if (isAdd)
                        InfoList.Add(info);*/

                    break;
                }
            }

            return info;

        }
    }
}
